package model
{
	import mx.collections.ArrayCollection;

	[Bindable]
	public class SlidePage
	{
		public  var elements:ArrayCollection = new ArrayCollection();
		
		
		public var pageWidth:int;
		
		public var pageHeight:int;
		
		private var elementAddedHandlers:ArrayCollection = new ArrayCollection();
		
		private var elementAddedHandlersScope:ArrayCollection = new ArrayCollection();
		
		private var elementRemovedHandlers:ArrayCollection = new ArrayCollection();
		
		private var elementRemovedHandlersScope:ArrayCollection = new ArrayCollection();
		
		public function SlidePage()
		{
		}
		
		public function add(newElement:PageElement):void {
			elements.addItem(newElement);
			for(var i:int=0; i<elementAddedHandlers.length; i++) {
				(elementAddedHandlers.getItemAt(i) as Function).call(elementAddedHandlersScope.getItemAt(i), newElement);
			}
		}
		
		public function addNewElementListener(scope:Object, method:Function):void {
			elementAddedHandlersScope.addItem(scope);
			elementAddedHandlers.addItem(method);
		}
		
		public function removeNewElementListener(scope:Object, method:Function):void {
			if(elementAddedHandlersScope.getItemIndex(scope) != -1) {
				elementAddedHandlersScope.removeItemAt(elementAddedHandlersScope.getItemIndex(scope));	
			}
			if(elementAddedHandlers.getItemIndex(method) != -1) {
				elementAddedHandlers.removeItemAt(elementAddedHandlers.getItemIndex(method));	
			}
			
		}
		
		public function addRemovedElementListener(scope:Object, method:Function):void {
			elementRemovedHandlersScope.addItem(scope);
			elementRemovedHandlers.addItem(method);
		}
		
		public function removeRemovedElementListener(scope:Object, method:Function):void {
			if(elementRemovedHandlersScope.getItemIndex(scope) != -1) {
				elementRemovedHandlersScope.removeItemAt(elementRemovedHandlersScope.getItemIndex(scope));
			}
			
			if(elementRemovedHandlers.getItemIndex(method) != -1) {
				elementRemovedHandlers.removeItemAt(elementRemovedHandlers.getItemIndex(method));
			}
		}
		
		public function removeElement(element:PageElement):void {
			var index:int = elements.getItemIndex(element);
			if(elements.getItemIndex(element) != -1) {
				elements.removeItemAt(index);	
			}
			for(var i:int=0; i<elementRemovedHandlers.length; i++) {
				(elementRemovedHandlers.getItemAt(i) as Function).call(elementRemovedHandlersScope.getItemAt(i), index);
			}
		}
	}
}